package com.qf.controller;

import com.qf.pojo.Goods;
import com.qf.pojo.PageBean;
import com.qf.service.GoodsService;
import com.qf.service.impl.GoodsServiceImpl;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Administrator on 2021/10/25.
 */
@WebServlet(urlPatterns = {"/goods", "/goods2"},
        initParams = {@WebInitParam(name = "rowsperpage", value = "2")},
        loadOnStartup = 1
)
public class GoodsController extends BaseController {

    private GoodsService goodsService = new GoodsServiceImpl();
    private int rowsPerPage = 1;

    @Override
    public void init(ServletConfig config) throws ServletException {
        //ServletConfig对象就是用读取初始化配置参数
        String rowsperpage = config.getInitParameter("rowsperpage");
        if (rowsperpage != null)
            this.rowsPerPage = Integer.valueOf(rowsperpage);
    }

    public String one(HttpServletRequest request, HttpServletResponse response) {
        //接收商品id
        String ids = request.getParameter("id");
        int id = Integer.valueOf(ids);
        //业务层
        Goods goods = goodsService.selectOneById(id);
        request.setAttribute("goods", goods);
        return "forward:details";
    }

    public String all(HttpServletRequest request, HttpServletResponse response) {

        //处理业务
        //index.jsp携带id参数，到达GoodsController，处理完数并传递到list.jsp
        //list.jsp携带id参数，到达GoodsController，处理id参数；list.jsp
        //根据一级类别查询所有商品并分页
        String id = request.getParameter("cid");//一级类别编号
        String id2 = request.getParameter("cid2");//二级类别编号
        String id3 = request.getParameter("cid3");//三级类别编号

        //接收分页参数:如果接收到null，则按第一页；如果接收到非null，则实际页码显示
        String pages = request.getParameter("page");//当前页
        int page = pages == null ? 1 : Integer.valueOf(pages);
        //分页参数
        PageBean pageBean = new PageBean();
        pageBean.setPage(page);//当前页??????
        pageBean.setRowsPerPage(rowsPerPage);//每页行数?????


        int cid = -1,level=0;
        if (id != null && id.trim().length()>0) {//trim去除字符串前后空格再获得长度
            //按一级类别编号查询商品
            cid = Integer.valueOf(id);//类别
            level=1;
            request.setAttribute("cid", cid);
        } else if (id2 != null && id2.trim().length()>0) {
            //二
            cid = Integer.valueOf(id2);//类别
            level=2;
            request.setAttribute("cid2", cid);
        } else if (id3 != null&& id3.trim().length()>0) {
            //三
            cid = Integer.valueOf(id3);//类别
            level=3;
            request.setAttribute("cid3", cid);
        }
        //调用方法
        pageBean.setRowsNum(goodsService.selectRow(cid,level));//设置总行数；但同时已经得到了总页数；
        List<Goods> glist =goodsService.selectAll(cid,level,pageBean);
        //存储数据
        request.setAttribute("glist", glist);//查询到的数据
        request.setAttribute("pb", pageBean);//当前页
        return "forward:list";
    }
}
